package com.yunxiangyue.server
import android.net.Uri
import com.yunxiangyue.musicplayer.Music
import okhttp3.FormBody
import okhttp3.RequestBody
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

class ConnectExample {


    val pc = PCServerConnectManager()


    //获取所有音乐文件的名称
    fun getAllMp3(musicMp3Type:String, callback: (Boolean, ByteArray) -> Unit) {
        val requestBody: RequestBody = FormBody.Builder()
            .add(OkHttpKeyUtil.KEY_GET_ALL_MP3,musicMp3Type)
            .build()
        pc.send(OkHttpKeyUtil.GET_ALL_MP3_URL, requestBody, callback)
    }


//    fun getMusicUri(mp3Data: ByteArray) {
//        val tempFile = File.createTempFile("audio", ".mp3", this.cacheDir)
//        tempFile.deleteOnExit() // 确保应用退出时删除文件
//
//        try {
//            val fos = FileOutputStream(tempFile)
//            fos.write(mp3Data)
//        } catch (e: IOException) {
//            e.printStackTrace()
//            // 处理错误
//        }
//        Uri.fromFile(tempFile)
//    }

    //根据音乐名称来获取对应的音乐文件
    fun getMusicByName(musicName:String, callback: (Boolean, ByteArray) ->Unit){
        val requestBody: RequestBody = FormBody.Builder()
            .add("musicName",musicName)    //请求体传入音乐名称
            .build()
        pc.send(OkHttpKeyUtil.GET_MUSIC_URI, requestBody, callback)
    }



//    const val KEY_USER_INFO_GENDER = "gender"
//    const val KEY_USER_INFO_BIRTHDAY ="birthday"
//    const val KEY_USER_INFO_REGION = "region"
//    const val KEY_USER_INFO_UNIVERSITY = "university"
//    const val KEY_USER_INFO_INTRO = "intro"
    //添加用户  注册用户的时候
    fun insertUser(phone: String, pwd: String, nike: String, userType: String, gender:String, birthday:String, region:String, university:String, intro:String, callback: (Boolean)->Unit) {
        //将传入的信息整合成RequestBody对象
        val requestBody: RequestBody = FormBody.Builder()
            .add(OkHttpKeyUtil.KEY_USER_INFO_PHONE,phone)
            .add(OkHttpKeyUtil.KEY_USER_INFO_PASSWORD,pwd)
            .add(OkHttpKeyUtil.KEY_USER_INFO_NAME,nike)
            .add(OkHttpKeyUtil.KEY_USER_INFO_TYPE,userType)
            .add(OkHttpKeyUtil. KEY_USER_INFO_GENDER,gender)
            .add(OkHttpKeyUtil. KEY_USER_INFO_BIRTHDAY,birthday)
            .add(OkHttpKeyUtil. KEY_USER_INFO_REGION,region)
            .add(OkHttpKeyUtil. KEY_USER_INFO_UNIVERSITY,university)
            .add(OkHttpKeyUtil. KEY_USER_INFO_INTRO,intro)
            .build()
        val callbackNew: (Boolean, ByteArray)->Unit = {result, _ ->
            callback(result)
        }
        pc.send(OkHttpKeyUtil.USER_INFO_URL, requestBody, callbackNew)
    }


    //更新用户，在 保存信息 时   用户第一次登录时还未设置昵称等，更新时要完善这些信息  按照更新字段的要求
    fun updateUser(phone: String, pwd: String, nike: String, userType: String, gender:String, birthday:String, region:String, university:String, intro:String, callback: (Boolean)->Unit) {
        //将传入的信息整合成RequestBody对象
        val requestBody: RequestBody = FormBody.Builder()
            .add(OkHttpKeyUtil.KEY_USER_INFO_PHONE,phone)
            .add(OkHttpKeyUtil.KEY_USER_INFO_PASSWORD,pwd)
            .add(OkHttpKeyUtil.KEY_USER_INFO_NAME,nike)
            .add(OkHttpKeyUtil.KEY_USER_INFO_TYPE,userType)
            .add(OkHttpKeyUtil. KEY_USER_INFO_GENDER,gender)
            .add(OkHttpKeyUtil. KEY_USER_INFO_BIRTHDAY,birthday)
            .add(OkHttpKeyUtil. KEY_USER_INFO_REGION,region)
            .add(OkHttpKeyUtil. KEY_USER_INFO_UNIVERSITY,university)
            .add(OkHttpKeyUtil. KEY_USER_INFO_INTRO,intro)
            .build()
        val callbackNew: (Boolean, ByteArray)->Unit = {result, _ ->
            callback(result)
        }
        pc.send(OkHttpKeyUtil.USER_INFO_UPDATE_URL, requestBody, callbackNew)
    }

}